<?php


function followers_init() {


	$actions = dirname(__FILE__) . '/actions';
	elgg_register_action('follow', "$actions/follow.php");
	elgg_register_action('unfollow', "$actions/unfollow.php");
	
	elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'followers_user_hover_menu');
	
	//extend the css - MK
	elgg_extend_view('css', 'followers/css');
	
	elgg_extend_view("profile/menu/actions", "followers/actions");
	
	if (isloggedin()) {
		
	
	//Extend topbar to add our link if needed - MK
	elgg_extend_view('elgg_topbar/extend', 'followers/topbar');
			
		
		$user = get_loggedin_user ();
		elgg_register_menu_item('filter', array(
			'name' => 'following',
			'href' => "/activity/following",
			'text' => "Following",
			'priority' => 500,
			'contexts' => array('activity'),
		));
	}
	
	
	global $CONFIG, $following_original_activity_page_handler;
	$following_original_activity_page_handler = $CONFIG->pagehandler['activity'];
	elgg_register_page_handler('activity', 'followers_activity_page_handler');
	
	add_widget_type('following', elgg_echo('widgets:following:name'), elgg_echo('widgets:following:description'));
	add_widget_type('followers', elgg_echo('widgets:followers:name'), elgg_echo('widgets:followers:description'));

	//This will let uesrs view their friend requests
	register_page_handler('followers', 'followers_page_handler');
}

function followers_activity_page_handler($segments, $handle) {
	switch ($segments[0]) {
		case 'following':
			require_once dirname(__FILE__) . '/pages/activity/following.php';
			return;
		default:
			global $following_original_activity_page_handler;
			return call_user_func($following_original_activity_page_handler, $segments, $handle);
	}
}

	function followers_page_handler($page) {
		include(dirname(__FILE__) . "/index.php"); 
	}

function followers_user_hover_menu($hook, $type, $return, $params) {
	$user = $params['entity'];

	if (isloggedin() && get_loggedin_userid() != $user->guid) {
		
		if (check_entity_relationship(get_loggedin_userid(), 'follower', $user->guid)) {
			
			//$url = elgg_add_action_tokens_to_url("action/unfollow?object_guid=$user->guid");
			
			//$item = new ElggMenuItem('unfollow', elgg_echo('unfollow:user'), $url);
			//add_submenu_item('unfollow', elgg_echo('unfollow:user'), $url);
			
			//put it in profile menu MK
		//	elgg_extend_view("profile/menu/links","followers/menu_unfollow");
			
			//	elgg_extend_view("profile/menu/links","followers/menu");
	
			
		} else {
			//$url = elgg_add_action_tokens_to_url("action/follow?object_guid=$user->guid");
			//$item = new ElggMenuItem('follow', elgg_echo('follow:user'), $url);
			
			
			
			//	elgg_extend_view("profile/menu/links","followers/menu_follow");
			
			//add_submenu_item('follow', elgg_echo('follow:user'), $url);
		}
		
//add_submenu_item(elgg_echo("friend_request:menu") . $extra, $CONFIG->wwwroot . "pg/friend_request/", "b");
		
		
		$item->setSection('action');
		$return[] = $item;
	}
	else{
		print_r("in the else");
	}

	return $return;
}

elgg_register_event_handler('init', 'system', 'followers_init');